extern "C"
{
    #include <xls.h>
}
#include <CExcelReader.h>


CExcelReader::CExcelValue::CExcelValue(STRING _strValue = "", DOUBLE _numValue = 0)
{
	this->m_strValue = _strValue;
	this->m_numValue = _numValue;
}


CExcelReader::CExcelValue::~CExcelValue()
{
	this->m_strValue = "";
	this->m_numValue = 0;
}

STRING CExcelReader::CExcelValue::getString()
{
	return this->m_strValue;
}

DOUBLE CExcelReader::CExcelValue::getNumber()
{
	return this->m_numValue;
}

CExcelReader::CExcelRow::CExcelRow(CExcelReader* _pReader = 0)
{
	ExcelRow my_tmp = ExcelRow();
	this->m_Row.swap(my_tmp);
	this->m_pReader = _pReader;
}

CExcelReader::CExcelRow::~CExcelRow()
{
	ExcelRow my_tmp = ExcelRow();
	this->m_Row.swap(my_tmp);
	this->m_pReader = 0;
}

bool CExcelReader::CExcelRow::Init(INT _RowIndex = 0)
{
	if (0 == m_pReader && 0 == m_pReader->getTablePoint())
	{
		return false;
	}
	xlsWorkSheet* tmp_pWS = (xlsWorkSheet*)(m_pReader->getTablePoint());
	for(int i = 0; i < tmp_pWS->rows.lastcol; i++)
	{
		STRING tmp_str;
		if (0 == tmp_pWS->rows.row[_RowIndex].cells.cell[i].str)
		{
			tmp_str = "";
		}
		else
		{
			tmp_str = tmp_pWS->rows.row[_RowIndex].cells.cell[i].str;
		}
		this->m_Row.push_back(CExcelReader::CExcelValue(tmp_str,tmp_pWS->rows.row[_RowIndex].cells.cell[i].d));
	}
	return true;
}

CExcelReader::CExcelValue& CExcelReader::CExcelRow::operator[](const DWORD _index)
{
	return this->m_Row[_index];
}

CExcelReader::CExcelReader()
{
	ExcelTable my_tmp = ExcelTable();
	this->m_Table.swap(my_tmp);
	this->m_pWB = 0;
	this->m_pWS = 0;
	this->m_MaxRow = 0;
	this->m_MaxColumn = 0;
}

CExcelReader::~CExcelReader()
{
	ExcelTable my_tmp = ExcelTable();
	this->m_Table.swap(my_tmp);
	if (0 != this->m_pWS)
	{
		xlsWorkSheet* tmp_pWS = (xlsWorkSheet*)this->m_pWS;
		free(tmp_pWS);
	}
	if (0 != this->m_pWB)
	{
		xlsWorkBook* tmp_pWB = (xlsWorkBook*)this->m_pWB;
		free(tmp_pWB);
	}
	this->m_pWB = 0;
	this->m_pWS = 0;
	this->m_MaxRow = 0;
	this->m_MaxColumn = 0;
}

bool CExcelReader::Init(STRING _FullFilePath, STRING _CharSet)
{
	xlsWorkBook* tmp_pWB = 0;
	tmp_pWB = xls_open ((char*)_FullFilePath.c_str(), (char*)_CharSet.c_str());
	if (0 == tmp_pWB)
	{
		ExcelTable my_tmp = ExcelTable();
		this->m_Table.swap(my_tmp);
		this->m_pWS = 0;
		return false;
	}
	this->m_pWB = (void*)tmp_pWB;
	return true;
}

bool CExcelReader::Init(STRING _FullFilePath, STRING _SheetName, STRING _CharSet)
{
	if (!Init(_FullFilePath, _CharSet))
	{
		return false;
	}
	if(!getWS(_SheetName))
	{
		return false;
	}
	return true;
}

bool CExcelReader::getWS(STRING _SheetName)
{
	if (0 != this->m_pWS)
	{
		xlsWorkSheet* tmp_pWS = (xlsWorkSheet*)this->m_pWS;
		free(tmp_pWS);
	}
	this->m_pWS = 0;
	if (0 == this->m_pWB)
	{
		return false;
	}
	xlsWorkBook* tmp_pWB = (xlsWorkBook*)this->m_pWB;
	xlsWorkSheet* tmp_pWS = 0;
	for (int i = 0; i < tmp_pWB->sheets.count; i++)
	{
		if(_SheetName == tmp_pWB->sheets.sheet[i].name)
		{
			tmp_pWS = xls_getWorkSheet(tmp_pWB,i);
			break;
		}
	}
	if (0 == tmp_pWS)
	{
		return false;
	}
	xls_parseWorkSheet(tmp_pWS);
	this->m_pWS = (void*)tmp_pWS;
	ExcelTable my_tmp = ExcelTable();
	this->m_Table.swap(my_tmp);
	for (int i = 0; i < tmp_pWS->rows.lastrow; i++)
	{
		CExcelRow tmp_row = CExcelRow(this);
		if(!tmp_row.Init(i))
		{
			ExcelTable my_tmp = ExcelTable();
			this->m_Table.swap(my_tmp);
			return false;
		}
		this->m_Table.push_back(tmp_row);
	}
	this->m_MaxRow = tmp_pWS->rows.lastrow;
	this->m_MaxColumn = tmp_pWS->rows.lastcol;
	return true;
}

INT CExcelReader::get_RowCount()
{
	return this->m_MaxRow;
}

INT CExcelReader::get_ColumnCount()
{
	return this->m_MaxColumn;
}

CExcelReader::CExcelRow& CExcelReader::operator [] (const DWORD _index)
{
	return this->m_Table[_index];
}

void* CExcelReader::getTablePoint()
{
	return this->m_pWS;
}

extern "C" _DLLFUNC PCExcelReader CreateNewPCExcelReader()
{
	return new CExcelReader();
}


